package com.open.capacity.user.controller;

import com.open.capacity.commons.Result;
import com.open.capacity.model.system.LoginAppUser;
import com.open.capacity.model.system.SysMenu;
import com.open.capacity.model.system.SysRole;
import com.open.capacity.user.model.desk.*;
import com.open.capacity.user.service.SysMenuService;
import com.open.capacity.utils.ColorConverter;
import com.open.capacity.utils.StringUtil;
import com.open.capacity.utils.SysUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;


@RestController
public class SysDesktopController {

    @Autowired
    private SysMenuService menuService;


    @GetMapping("/homes/getDesktop")
    public Result<Desktop> getDesktop() {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser();

        //系统功能菜单
        Set<SysRole> roles = loginAppUser.getSysRoles();
        List<SysMenu> menus = new ArrayList<>();
        if (loginAppUser.getId().equals("1")) {
            Map<String, Object> params = new HashMap<>();
            menus = menuService.findMenus(params);
        } else if (!CollectionUtils.isEmpty(roles)) {
            menus = menuService.findByRoles(roles.parallelStream().filter(item -> item.getStatus().equals(1)).map(SysRole::getId).collect(Collectors.toSet()));
        }

        menus = menus.stream().distinct().collect(Collectors.toList());


        Desktop desktop = new Desktop();

        //configs
        desktop.setConfigs(initConfigs());

        //apps
        desktop.setApps(initApps(menus));

        //shortcuts
        desktop.setShortcuts(initShortcuts(menus));

        //Tile
        desktop.setTiles(initTiles());

        //startMenu
        desktop.setStartMenu(initStartMenu(menus));

        return Result.succeed(desktop);
    }

    private Config initConfigs() {
        //配置
        Config config = new Config();
        config.setTopTaskBar(false);
        config.setSound(false);
        config.setWallpaper("./res/img/wallpapers/bg1.jpg");
        config.setWallpaperBlur(false);
        config.setWallpaperSlide(false);
        config.setWallpaperSlideRandom(true);
        config.setWallpaperSlideItv(1);
        config.setWallpaperSlideTime(1519442460788L);
        config.setWallpaperSlideIndex(8);
        config.setOpenMax(9);
        config.setIdCounter(170);
        config.setThemeColor("rgba(2,35,64,1)");
        config.setAutoThemeColor(true);
        config.setPathRes("./res");
        config.setShortcutsSortAuto(true);
        config.setDebug(false);
        config.setWinBlur(true);
        config.setWallpapers(Arrays.asList(new Config.Wallpaper[]{
                new Config.Wallpaper("", null),
                new Config.Wallpaper("./res/img/wallpapers/bg1.jpg", "./res/img/wallpapers/bg1_1.jpg"),
                new Config.Wallpaper("./res/img/wallpapers/bg2.jpg", "./res/img/wallpapers/bg2_1.jpg"),
                new Config.Wallpaper("./res/img/wallpapers/bg3.jpg", "./res/img/wallpapers/bg3_1.jpg"),
                new Config.Wallpaper("./res/img/wallpapers/bg4.jpg", "./res/img/wallpapers/bg4_1.jpg"),
                new Config.Wallpaper("./res/img/wallpapers/bg5.jpg", "./res/img/wallpapers/bg5_1.jpg"),
                new Config.Wallpaper("./res/img/wallpapers/bg6.jpg", "./res/img/wallpapers/bg6_1.jpg"),
                new Config.Wallpaper("./res/img/wallpapers/bg7.jpg", "./res/img/wallpapers/bg7_1.jpg"),
                new Config.Wallpaper("./res/img/wallpapers/bg8.jpg", "./res/img/wallpapers/bg8_1.jpg"),
                new Config.Wallpaper("./res/img/wallpapers/bg9.jpg", "./res/img/wallpapers/bg9_1.jpg"),
                new Config.Wallpaper("./res/img/wallpapers/bg10.jpg", "./res/img/wallpapers/bg10_1.jpg"),
                new Config.Wallpaper("./res/img/wallpapers/bg11.jpg", "./res/img/wallpapers/bg11_1.jpg"),
                new Config.Wallpaper("./res/img/wallpapers/bg12.jpg", "./res/img/wallpapers/bg12_1.jpg"),
                new Config.Wallpaper("./res/img/wallpapers/bg13.jpg", "./res/img/wallpapers/bg13_1.jpg"),
                new Config.Wallpaper("./res/img/wallpapers/bg14.jpg", "./res/img/wallpapers/bg14_1.jpg"),

        }));
        return config;
    }

    private List<Item> initShortcuts(List<SysMenu> menus) {
        List<Item> shortcuts = new ArrayList<>();
        shortcuts.add(new Item("yl-browser", "浏览器", null, ""));
        shortcuts.add(new Item("yl-app-home", "官网", null, ""));
        shortcuts.add(new Item("yl-app-calculator", "科学计算器", null, ""));

        if (menus.stream().filter(item -> item.getId().equals("20201")).count() > 0) {
            shortcuts.add(new Item("20101", "文件中心", null, ""));
        }
        if (menus.stream().filter(item -> item.getId().equals("20102")).count() > 0) {
            shortcuts.add(new Item("20102", "合同管理", null, ""));
        }
        if (menus.stream().filter(item -> item.getId().equals("20301")).count() > 0) {
            shortcuts.add(new Item("20301", "邮件中心", null, ""));
        }
        if (menus.stream().filter(item -> item.getId().equals("20201")).count() > 0) {
            shortcuts.add(new Item("20201", "发票管理", null, ""));
        }
        if (menus.stream().filter(item -> item.getId().equals("20202")).count() > 0) {
            shortcuts.add(new Item("20202", "发票识别", null, ""));
        }
        if (menus.stream().filter(item -> item.getId().equals("60401")).count() > 0) {
            shortcuts.add(new Item("60401", "流程设计", null, ""));
        }
        if (menus.stream().filter(item -> item.getId().equals("60301")).count() > 0) {
            shortcuts.add(new Item("60301", "表单设计", null, ""));
        }
        if (menus.stream().filter(item -> item.getId().equals("80201")).count() > 0) {
            shortcuts.add(new Item("80201", "大屏展示", null, ""));
        }
        if (menus.stream().filter(item -> item.getId().equals("60501")).count() > 0) {
            shortcuts.add(new Item("60501", "我的任务", null, ""));
        }

        if (menus.stream().filter(item -> item.getId().equals("602")).count() > 0) {
            Item group = new Item();
            group.setTitle("费用报销");
            List<SysMenu> fees = menus.stream().filter(r -> r.getId().startsWith("602") && StringUtil.isNotEmpty(r.getPath())).collect(Collectors.toList());
            group.setChildren(fees.stream().map(f -> new Item(f.getId(), f.getName(), null, "")).collect(Collectors.toList()));
            shortcuts.add(group);
        }

        if (menus.stream().filter(item -> item.getId().equals("401")).count() > 0) {
            Item group = new Item();
            group.setTitle("车辆管理");
            List<SysMenu> cars = menus.stream().filter(r -> r.getParentId().equals("401")).collect(Collectors.toList());
            group.setChildren(cars.stream().map(f -> new Item(f.getId(), f.getName(), null, "")).collect(Collectors.toList()));
            shortcuts.add(group);
        }

        if (menus.stream().filter(item -> item.getId().equals("502")).count() > 0) {
            Item group = new Item();
            group.setTitle("项目管理");
            List<SysMenu> projects = menus.stream().filter(r -> r.getParentId().equals("502")).collect(Collectors.toList());
            group.setChildren(projects.stream().map(f -> new Item(f.getId(), f.getName(), null, "")).collect(Collectors.toList()));
            shortcuts.add(group);
        }

        if (menus.stream().filter(item -> item.getId().equals("503")).count() > 0) {
            Item group = new Item();
            group.setTitle("预算管理");
            List<SysMenu> budgets = menus.stream().filter(r -> r.getParentId().equals("503")).collect(Collectors.toList());
            group.setChildren(budgets.stream().map(f -> new Item(f.getId(), f.getName(), null, "")).collect(Collectors.toList()));
            shortcuts.add(group);
        }

        if (menus.stream().filter(item -> item.getId().equals("701")).count() > 0) {
            Item group = new Item();
            group.setTitle("数据标准库");
            List<SysMenu> stands = menus.stream().filter(r -> r.getParentId().equals("701")).collect(Collectors.toList());
            group.setChildren(stands.stream().map(f -> new Item(f.getId(), f.getName(), null, "")).collect(Collectors.toList()));
            shortcuts.add(group);
        }

        if (menus.stream().filter(item -> item.getId().equals("901")).count() > 0) {
            Item group = new Item();
            group.setTitle("票据打印");
            List<SysMenu> prints = menus.stream().filter(r -> r.getParentId().equals("901")).collect(Collectors.toList());
            group.setChildren(prints.stream().map(f -> new Item(f.getId(), f.getName(), null, "")).collect(Collectors.toList()));
            shortcuts.add(group);
        }

        return shortcuts;
    }

    private List<Tile> initTiles() {
        List<Tile> tiles = new ArrayList<>();
        Tile tile = new Tile();
        tile.setTitle("常用");

        tile.setData(Arrays.asList(
                new Tile.Title(0, 6, 2, 2, "yl-system", "系统设置", "91", null, ""),
                new Tile.Title(0, 4, 4, 2, "yl-dynamic-wallpaper", "蓝色动态三角动态壁纸", "110", null, ""),
                new Tile.Title(4, 4, 2, 2, "yl-app-home", "官网", "146", null, ""),
                new Tile.Title(0, 0, 2, 4, "yl-color-picker", "取色器", "147", null, ""),
                new Tile.Title(2, 0, 4, 4, "star-war-wallpaper", "星球大战壁纸", "161", null, ""),
                new Tile.Title(2, 6, 4, 2, "yl-app-calculator", "计算器", "158", null, "")));
        tiles.add(tile);
        return tiles;
    }

    private StartMenu initStartMenu(List<SysMenu> menus) {
        StartMenu startMenu = new StartMenu();
        startMenu.setHeight(494);
        startMenu.setWidth(620);
        startMenu.setSidebar(Arrays.asList(
                new Menu("app-my", "用户信息", null, ""),
                new Menu("yl-system", "系统设置", null, ""),
                new Menu("app-logout", "注销登录", null, "")
        ));

        Map<String, Menu> menu = new HashMap<>();

        List<SysMenu> orgs = menus.stream().filter(r -> r.getParentId().equals("101")).collect(Collectors.toList());
        Menu item_org = new Menu("101", "组织架构管理", null, "");
        Map<String, Menu> map_org = new HashMap();
        orgs.forEach(item -> map_org.put(item.getId(), new Menu(item.getId(), item.getName(), null, "")));
        item_org.setChildren(map_org);
        menu.put("101", item_org);

        List<SysMenu> services = menus.stream().filter(r -> r.getParentId().equals("102")).collect(Collectors.toList());
        Menu item_service = new Menu("102", "后台服务管理", null, "");
        Map<String, Menu> map_service = new HashMap();
        services.forEach(item -> map_service.put(item.getId(), new Menu(item.getId(), item.getName(), null, "")));
        item_service.setChildren(map_service);
        menu.put("102", item_service);

        List<SysMenu> admins = menus.stream().filter(r -> r.getParentId().equals("103")).collect(Collectors.toList());
        Menu item_admin = new Menu("103", "系统管理", null, "");
        Map<String, Menu> map_admin = new HashMap();
        admins.forEach(item -> map_admin.put(item.getId(), new Menu(item.getId(), item.getName(), null, "")));
        item_admin.setChildren(map_admin);
        menu.put("103", item_admin);

        startMenu.setMenu(menu);
        return startMenu;
    }

    private Map<String, App> initApps(List<SysMenu> menus) {
        Map<String, App> apps = new HashMap<>();
        setSystemApps(apps);
        menus = menus.stream().filter(item -> StringUtil.isNotEmpty(item.getPath())).collect(Collectors.toList());
        for (SysMenu menu : menus) {
            App app = new App();
            app.setAddressBar(false);
            app.setAutoRun(0);
            app.setBackground(false);
            app.setBadge(0);
            app.setDesc(menu.getDescription());
            app.setIcon(new App.Icon("fa", ColorConverter.RandomRGB(), menu.getCss().replace("fa fa-", "")));
            app.setOpenMode("normal");
            app.setPlugin(false);
            app.setPosition(new App.Position("x*0.08", "y*0.01", true, true, true));
            app.setVersion("1.0.0");
            app.setPoweredBy("tonghui");
            app.setResizable(true);
            app.setSingle(false);
            app.setSize(new App.Size("x*0.9", "y-80"));
            app.setTitle(menu.getName());
            app.setUrl(menu.getPath());
            app.setCustomTile("");
            app.setUrlRandomToken(true);
            apps.put(menu.getId(), app);
        }
        return apps;
    }

    //系统app
    private void setSystemApps(Map<String, App> apps) {
        App app_my = new App();
        app_my.setAddressBar(false);
        app_my.setAutoRun(0);
        app_my.setBackground(false);
        app_my.setBadge(0);
        app_my.setIcon(new App.Icon("fa", "#436fde", "user-circle"));
        app_my.setOpenMode("normal");
        app_my.setPlugin(false);
        app_my.setPosition(new App.Position("x*0.05", "y*0.05", true, true, true));
        app_my.setVersion("1.0.0");
        app_my.setPoweredBy("YLUI");
        app_my.setResizable(true);
        app_my.setSingle(true);
        app_my.setSize(new App.Size("920", "590"));
        app_my.setTitle("用户信息");
        app_my.setUrl("/pages/system/my/index.html");
        app_my.setCustomTile("");
        app_my.setUrlRandomToken(true);
        apps.put("app-my", app_my);

        App app_logout = new App();
        app_logout.setAddressBar(false);
        app_logout.setAutoRun(0);
        app_logout.setBackground(true);
        app_logout.setBadge(0);
        app_logout.setIcon(new App.Icon("fa", "#436fde", "power-off"));
        app_logout.setOpenMode("min");
        app_logout.setPlugin(false);
        app_logout.setPosition(new App.Position("x", "y", true, true, false));
        app_logout.setVersion("1.0.0");
        app_logout.setPoweredBy("YLUI");
        app_logout.setResizable(true);
        app_logout.setSingle(true);
        app_logout.setSize(new App.Size("0", "0"));
        app_logout.setTitle("注销登录");
        app_logout.setUrl("/login.html");
        app_logout.setCustomTile("");
        app_logout.setUrlRandomToken(true);
        apps.put("app-logout", app_logout);

        App yl_system = new App();
        yl_system.setAddressBar(false);
        yl_system.setAutoRun(0);
        yl_system.setBackground(false);
        yl_system.setBadge(0);
        yl_system.setIcon(new App.Icon("fa", "#436fde", "gear"));
        yl_system.setOpenMode("normal");
        yl_system.setPlugin(false);
        yl_system.setPosition(new App.Position("x*0.05", "y*0.05", true, true, true));
        yl_system.setVersion("1.0.0");
        yl_system.setPoweredBy("YLUI");
        yl_system.setResizable(true);
        yl_system.setSingle(true);
        yl_system.setSize(new App.Size("920", "590"));
        yl_system.setTitle("系统设置");
        yl_system.setUrl("./res/apps/yl-system/index.html");
        yl_system.setCustomTile("");
        yl_system.setUrlRandomToken(true);
        apps.put("yl-system", yl_system);

        App yl_color_picker = new App();
        yl_color_picker.setAddressBar(false);
        yl_color_picker.setAutoRun(0);
        yl_color_picker.setBackground(false);
        yl_color_picker.setBadge(0);
        yl_color_picker.setIcon(new App.Icon("fa", "#3289c7", "eyedropper"));
        yl_color_picker.setOpenMode("normal");
        yl_color_picker.setPlugin(false);
        yl_color_picker.setPosition(new App.Position("(x-207)/2", "(y-239)/2", true, true, false));
        yl_color_picker.setVersion("1.0.0");
        yl_color_picker.setPoweredBy("Yuri2");
        yl_color_picker.setResizable(false);
        yl_color_picker.setSingle(true);
        yl_color_picker.setSize(new App.Size("207", "239"));
        yl_color_picker.setTitle("取色器");
        yl_color_picker.setUrl("./res/apps/colorPicker/colorPicker.html");
        yl_color_picker.setCustomTile("");
        yl_color_picker.setUrlRandomToken(true);
        apps.put("yl-color-picker", yl_color_picker);

        App yl_browser = new App();
        yl_browser.setAddressBar(false);
        yl_browser.setAutoRun(0);
        yl_browser.setBackground(false);
        yl_browser.setBadge(0);
        yl_browser.setIcon(new App.Icon("fa", "#3a55c2", "internet-explorer"));
        yl_browser.setOpenMode("normal");
        yl_browser.setPlugin(false);
        yl_browser.setPosition(new App.Position("x*0.1", "y*0.1", true, true, true));
        yl_browser.setVersion("1.0.0");
        yl_browser.setPoweredBy("YLUI");
        yl_browser.setResizable(true);
        yl_browser.setSingle(true);
        yl_browser.setSize(new App.Size("x*0.8", "y*0.8-80"));
        yl_browser.setTitle("浏览器");
        yl_browser.setUrl("./res/apps/browser/index.html");
        yl_browser.setCustomTile("");
        yl_browser.setUrlRandomToken(true);
        apps.put("yl-browser", yl_browser);

        App yl_app_home = new App();
        yl_app_home.setAddressBar(false);
        yl_app_home.setAutoRun(0);
        yl_app_home.setBackground(false);
        yl_app_home.setBadge(0);
        yl_app_home.setIcon(new App.Icon("fa", "#6b96cf", "yoast"));
        yl_app_home.setOpenMode("normal");
        yl_app_home.setPlugin(false);
        yl_app_home.setPosition(new App.Position("x*0.1", "y*0.1", true, true, true));
        yl_app_home.setVersion("1.0.0");
        yl_app_home.setPoweredBy("YLUI");
        yl_app_home.setResizable(true);
        yl_app_home.setSingle(true);
        yl_app_home.setSize(new App.Size("x*0.8", "y*0.8-80"));
        yl_app_home.setTitle("官方网站");
        yl_app_home.setUrl("http://tonghuisoftservers.com");
        yl_app_home.setCustomTile("");
        yl_app_home.setUrlRandomToken(true);
        apps.put("yl-app-home", yl_app_home);

        App yl_app_calculator = new App();
        yl_app_calculator.setAddressBar(false);
        yl_app_calculator.setAutoRun(0);
        yl_app_calculator.setBackground(false);
        yl_app_calculator.setBadge(0);
        yl_app_calculator.setIcon(new App.Icon("fa", "#375980", "calculator"));
        yl_app_calculator.setOpenMode("normal");
        yl_app_calculator.setPlugin(false);
        yl_app_calculator.setPosition(new App.Position("x*0.5", "y*0.05", true, true, true));
        yl_app_calculator.setVersion("1.0.0");
        yl_app_calculator.setPoweredBy("YLUI");
        yl_app_calculator.setResizable(true);
        yl_app_calculator.setSingle(true);
        yl_app_calculator.setSize(new App.Size("324", "496"));
        yl_app_calculator.setTitle("科学计算器");
        yl_app_calculator.setUrl("./res/apps/calculator/index.html");
        yl_app_calculator.setCustomTile("");
        yl_app_calculator.setUrlRandomToken(true);
        apps.put("yl-app-calculator", yl_app_calculator);

        App yl_dynamic_wallpaper = new App();
        yl_dynamic_wallpaper.setAddressBar(false);
        yl_dynamic_wallpaper.setAutoRun(0);
        yl_dynamic_wallpaper.setBackground(true);
        yl_dynamic_wallpaper.setBadge(0);
        yl_dynamic_wallpaper.setIcon(new App.Icon("fa", "#461e38", "image"));
        yl_dynamic_wallpaper.setOpenMode("normal");
        yl_dynamic_wallpaper.setPlugin(true);
        yl_dynamic_wallpaper.setPosition(new App.Position("-2", "-2", true, true, false));
        yl_dynamic_wallpaper.setVersion("1.0.0");
        yl_dynamic_wallpaper.setPoweredBy("YLUI");
        yl_dynamic_wallpaper.setResizable(false);
        yl_dynamic_wallpaper.setSingle(true);
        yl_dynamic_wallpaper.setSize(new App.Size("x+4", "y+4"));
        yl_dynamic_wallpaper.setTitle("蓝色三角动态壁纸");
        yl_dynamic_wallpaper.setUrl("./res/apps/dynamic-wallpaper/index.html");
        yl_dynamic_wallpaper.setCustomTile("");
        yl_dynamic_wallpaper.setUrlRandomToken(true);
        apps.put("yl-dynamic-wallpaper", yl_dynamic_wallpaper);

        App star_war_wallpaper = new App();
        star_war_wallpaper.setAddressBar(false);
        star_war_wallpaper.setAutoRun(0);
        star_war_wallpaper.setBackground(true);
        star_war_wallpaper.setBadge(0);
        star_war_wallpaper.setIcon(new App.Icon("fa", "#b24c8e", "image"));
        star_war_wallpaper.setOpenMode("normal");
        star_war_wallpaper.setPlugin(true);
        star_war_wallpaper.setPosition(new App.Position("-2", "-2", true, true, false));
        star_war_wallpaper.setVersion("1.0.0");
        star_war_wallpaper.setPoweredBy("YLUI");
        star_war_wallpaper.setResizable(false);
        star_war_wallpaper.setSingle(true);
        star_war_wallpaper.setSize(new App.Size("x+4", "y+4"));
        star_war_wallpaper.setTitle("星球大战动态壁纸");
        star_war_wallpaper.setUrl("./res/apps/star-war-wallpaper/index.html");
        star_war_wallpaper.setCustomTile("");
        star_war_wallpaper.setUrlRandomToken(true);
        apps.put("star-war-wallpaper", star_war_wallpaper);

//        App yl_clock_plugin = new App();
//        yl_clock_plugin.setAddressBar(true);
//        yl_clock_plugin.setAutoRun(2);
//        yl_clock_plugin.setBackground(true);
//        yl_clock_plugin.setBadge(0);
//        yl_clock_plugin.setIcon(new App.Icon("fa", "rgb(238,37,96)", "clock-o"));
//        yl_clock_plugin.setOpenMode("normal");
//        yl_clock_plugin.setPlugin(true);
//        yl_clock_plugin.setPosition(new App.Position("0", "0", false, false, false));
//        yl_clock_plugin.setVersion("1.0.0");
//        yl_clock_plugin.setPoweredBy("YLUI");
//        yl_clock_plugin.setResizable(true);
//        yl_clock_plugin.setSingle(true);
//        yl_clock_plugin.setSize(new App.Size("336", "383"));
//        yl_clock_plugin.setTitle("创意时钟");
//        yl_clock_plugin.setUrl("./res/apps/React Morph Clock/index.html");
//        yl_clock_plugin.setCustomTile("");
//        yl_clock_plugin.setUrlRandomToken(true);
//        apps.put("yl-clock-plugin", yl_clock_plugin);
    }
}
